package com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.firstdemo.network.api.addProblem.AddProblemApi
import com.example.firstdemo.network.api.addProblem.data.ProblemDictData
import com.example.firstdemo.network.service.NetworkException
import com.example.firstdemo.ui.screens.pages.addProblem.addProblem.selectProblem.data.SelectProblemUIState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class SelectProblemViewModel : ViewModel() {
    private val _uiState = MutableStateFlow(SelectProblemUIState())
    val uiState = _uiState.asStateFlow()

    var problemList: List<ProblemDictData> = emptyList()
    fun getProblemList() {
        viewModelScope.launch {
            try {
                problemList = AddProblemApi.getProblem().data ?: emptyList()
                _uiState.value = _uiState.value.copy(
                    problemList = problemList
                )
            } catch (_: NetworkException) {
            }
        }
    }

    fun filterProblem(carriageNo: String) {
        _uiState.value = _uiState.value.copy(
            problemList = problemList.filter { it.label.contains(carriageNo) }
        )
    }
}